package com.ptu.controller.admin;

import com.ptu.dto.UserDTO;
import com.ptu.entity.Product;
import com.ptu.entity.ProductStatusDTO;
import com.ptu.server.OSSService;
import com.ptu.server.ProductServer;
import com.ptu.util.PageResult;
import com.ptu.util.Result;
import com.ptu.util.UserHolder;
import com.ptu.vo.ProductVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController("adminProductController")
@RequestMapping("/admin/product")
@Tag(name = "管理员商品模块")
public class ProductController {

    @Autowired
    ProductServer productServer;

    @Autowired
    OSSService ossService;

    @RequestMapping("/list")
    @Operation(summary = "商品列表")
    public Result<PageResult> list(String name, Integer categoryId, Integer pageNum, Integer pageSize) {
        System.out.println("分页查询用户列表");
        PageResult  pageResult = productServer.getProuctList(name, categoryId, pageNum, pageSize);
        return Result.success(pageResult);
    }

    @RequestMapping("/detail")
    @Operation(summary = "商品详情")
    public Result<Product> detail(Long id) {
        System.out.println("商品详情");
        Product product = productServer.adminGetDetailById(id);
        return Result.success(product);
    }

    @RequestMapping("/update")
    @Operation(summary = "修改商品信息")
    public Result update(@RequestBody Product product) {
        System.out.println("修改商品信息");
        productServer.adminUpdate(product);
        return Result.success();
    }

    @RequestMapping("/add")
    @Operation(summary = "新增商品")
    public Result add(@RequestBody Product product) {
        System.out.println("新增商品");
        productServer.adminAdd(product);
        return Result.success();
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Long id) {
        System.out.println("删除商品");
        productServer.delete(id);
        return Result.success();
    }

    @Operation(summary = "停售起售商品")
    @PostMapping("/start")
    public Result start(@RequestParam Long id) {  // 添加@RequestParam注解
        System.out.println("停售起售商品"+ id);
        productServer.start(id);
        return Result.success();
    }

    @Operation(summary = "管理员上传商品图片")
    @PostMapping("/upload/product")
    public Result<List<String>> uploadProductImages(
            @RequestParam("files") MultipartFile[] files,
            @RequestParam(value = "productId", required = false) Long productId) {

        System.out.println("上传商品图片");


        // 验证文件数量
        if (files == null || files.length == 0) {
            return Result.error("请上传至少一张图片");
        }
        if (files.length > 1) { // 限制最多5张图片
            return Result.error("最多上传1张图片");
        }

        List<String> imageUrls = new ArrayList<>();

        try {
            for (MultipartFile file : files) {
                // 验证文件类型
                String contentType = file.getContentType();
                if (contentType == null || !contentType.startsWith("image/")) {
                    return Result.error("请上传图片文件");
                }


                // 上传到OSS，使用不同的文件夹区分商品图片
                String imageUrl = ossService.uploadProductImage(file, "products/");
                imageUrls.add(imageUrl);
            }

            // 如果有productId，可以在这里更新商品图片关联
            if (productId != null) {
                // 调用商品服务更新图片
                // productService.updateProductImages(productId, imageUrls);
            }

            return Result.success(imageUrls);
        } catch (IOException e) {
            System.out.println("上传失败" + e);
            return Result.error("上传失败");
        }
    }

}
